package xmlParse;

import java.util.Collections;
import java.util.List;
import java.util.Vector;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

import Tools.BitmapTool;


public class FntPaint {
	
	private String m_string;
	private int m_strX;
	private int m_strY;
	private SAXPersonService saxPersonService;
	private List<FntInfo> fInfos;
	private Bitmap fntBitmap;
	private Vector<Integer> savedInfo = new Vector<Integer>(); //
	
	
	public FntPaint(String wordString,int x,int y)
	{
		this.m_string = wordString;
		this.m_strX = x;
		this.m_strY = y;
		initXML();
	}
	
	
	private void initXML()
	{
		if(saxPersonService == null)
		{
			saxPersonService = new SAXPersonService();
			fntBitmap = BitmapTool.getImageFromAssetFile("fnt/chin-0.png");
			fInfos = saxPersonService.readXML(BitmapTool.getAssetsFromFile("fnt/chin.xml"));
		}
	
		parseStr();
	}

	
	private void parseStr()
	{
		for (int i = 0; i < m_string.length(); i++) {
			if(i<m_string.length()-1)
				getFntcharPos(m_string.substring(i, i+1));
			else
				getFntcharPos(m_string.substring(m_string.length()-1));
		}
	}
	
	int orgX = -1;
	int orgY = -1;
	int wordW = -1;
	int wordH = -1;
	private void getFntcharPos(String justoneString)
	{
		for (int i = 0; i < fInfos.size(); i++) {
			if(justoneString.equals(fInfos.get(i).getFirString()))
			{
				orgX = fInfos.get(i).getWord_x();
				orgY = fInfos.get(i).getWord_y();
				wordW = fInfos.get(i).getWord_W();
				wordH = fInfos.get(i).getWord_H();
				 savedInfo.add(new Integer(orgX));
				 savedInfo.add(new Integer(orgY));
				 savedInfo.add(new Integer(wordW));
				 savedInfo.add(new Integer(wordH));
				 break;
			}
		}
	}
	
	public void drawFntWord(Canvas canvas,Paint paint)
	{
		for (int i = 0; i < savedInfo.size()/4; i++) {
			BitmapTool.drawRegoin(canvas, paint, fntBitmap, 0,m_strX+15*i, m_strY, savedInfo.get(4*i), savedInfo.get(4*i+1), savedInfo.get(4*i+2), savedInfo.get(4*i+3));
		}
	}
	
	
	
}
